package com.ailk.cuc.autotestbg.common.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class MessageUtil {
	public String compress(String str) throws IOException {
		if (str == null || str.length() == 0) {
			return str;
		}
		BASE64Encoder base64encoder = new BASE64Encoder();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		GZIPOutputStream gzip = new GZIPOutputStream(out);
		gzip.write(str.getBytes("UTF-8"));
		gzip.close();
		System.out.println("GZIP" + out.toString("UTF-8"));
		return base64encoder.encode(out.toByteArray());
	}

	/**
	 * @param str
	 * @return uncompressed string
	 * @throws IOException
	 * 
	 */
	public static String uncompress(String srcStr) throws IOException {

		if (srcStr == null || srcStr.length() == 0) {
			return srcStr;
		}
		BASE64Decoder base64decoder = new BASE64Decoder();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] bytes = base64decoder.decodeBuffer(srcStr);
		ByteArrayInputStream in = new ByteArrayInputStream(bytes);
		GZIPInputStream gunzip = new GZIPInputStream(in);
		byte[] buffer = new byte[256];
		int n;
		while ((n = gunzip.read(buffer)) >= 0) {
			out.write(buffer, 0, n);
		}
		return out.toString("GBK");
	}

//	public String uncompress1(String srcStr) throws Exception {
//		ByteArrayOutputStream out = new ByteArrayOutputStream();
//		ByteArrayInputStream in = new ByteArrayInputStream(srcStr.getBytes());
//		GZIPInputStream gunzip = new GZIPInputStream(in);
//		byte[] buffer = new byte[256];
//		int n;
//		while ((n = gunzip.read(buffer)) >= 0) {
//			out.write(buffer, 0, n);
//		}
//		String s = out.toString("UTF-8");
//		return null;
//	}

//	public static void main(String[] args) {
//		MessageUtil u = new MessageUtil();
//		// String s =
//		// "<UNI_BSS_BODY><QRY_CDR_OF_VA_INFO_RSP><RESP_DESC>�ɹ�</RESP_DESC><CDR_INFO><BILL_NUMBER>15657177218</BILL_NUMBER><SERVICE_TYPE>32</SERVICE_TYPE><START_TIME>20101004000000</START_TIME><TOTAL_FEE>0</TOTAL_FEE><BUSINESS_CODE>32360002</BUSINESS_CODE><SP_NAME>���ݹ�����湫˾</SP_NAME><MT_FEE> </MT_FEE><XFEE> </XFEE><SP_CODE>62910</SP_CODE><SEND_TYPE>1</SEND_TYPE><BILL_TYPE>2</BILL_TYPE><DURATION>0</DURATION><SERVICE_NAME>��Ե����ҵ��</SERVICE_NAME><MT_COUNT>0</MT_COUNT><OTHER_PARTY>13606609996</OTHER_PARTY></CDR_INFO><CDR_INFO><BILL_NUMBER>15657177218</BILL_NUMBER><SERVICE_TYPE>32</SERVICE_TYPE><START_TIME>20101004000000</START_TIME><TOTAL_FEE>0</TOTAL_FEE><BUSINESS_CODE>32360002</BUSINESS_CODE><SP_NAME>���ݹ�����湫˾</SP_NAME><MT_FEE> </MT_FEE><XFEE> </XFEE><SP_CODE>62910</SP_CODE><SEND_TYPE>1</SEND_TYPE><BILL_TYPE>2</BILL_TYPE><DURATION>0</DURATION><SERVICE_NAME>��Ե����ҵ��</SERVICE_NAME><MT_COUNT>0</MT_COUNT><OTHER_PARTY>13868068762</OTHER_PARTY></CDR_INFO><CDR_INFO><BILL_NUMBER>15657177218</BILL_NUMBER><SERVICE_TYPE>32</SERVICE_TYPE><START_TIME>20101004000000</START_TIME><TOTAL_FEE>0</TOTAL_FEE><BUSINESS_CODE>32360002</BUSINESS_CODE><SP_NAME>���ݹ�����湫˾</SP_NAME><MT_FEE> </MT_FEE><XFEE> </XFEE><SP_CODE>62910</SP_CODE><SEND_TYPE>1</SEND_TYPE><BILL_TYPE>2</BILL_TYPE><DURATION>0</DURATION><SERVICE_NAME>��Ե����ҵ��</SERVICE_NAME><MT_COUNT>0</MT_COUNT><OTHER_PARTY>13634170668</OTHER_PARTY></CDR_INFO><CDR_INFO><BILL_NUMBER>15657177218</BILL_NUMBER><SERVICE_TYPE>32</SERVICE_TYPE><START_TIME>20101004000000</START_TIME><TOTAL_FEE>0</TOTAL_FEE><BUSINESS_CODE>32360002</BUSINESS_CODE><SP_NAME>���ݹ�����湫˾</SP_NAME><MT_FEE> </MT_FEE><XFEE> </XFEE><SP_CODE>62910</SP_CODE><SEND_TYPE>1</SEND_TYPE><BILL_TYPE>2</BILL_TYPE><DURATION>0</DURATION><SERVICE_NAME>��Ե����ҵ��</SERVICE_NAME><MT_COUNT>0</MT_COUNT><OTHER_PARTY>13606609996</OTHER_PARTY></CDR_INFO><CDR_INFO><BILL_NUMBER>15657177218</BILL_NUMBER><SERVICE_TYPE>32</SERVICE_TYPE><START_TIME>20101004000000</START_TIME><TOTAL_FEE>0</TOTAL_FEE><BUSINESS_CODE>32360002</BUSINESS_CODE><SP_NAME>���ݹ�����湫˾</SP_NAME><MT_FEE> </MT_FEE><XFEE> </XFEE><SP_CODE>62910</SP_CODE><SEND_TYPE>1</SEND_TYPE><BILL_TYPE>2</BILL_TYPE><DURATION>0</DURATION><SERVICE_NAME>��Ե����ҵ��</SERVICE_NAME><MT_COUNT>0</MT_COUNT><OTHER_PARTY>13868068762</OTHER_PARTY></CDR_INFO><CDR_INFO><BILL_NUMBER>15657177218</BILL_NUMBER><SERVICE_TYPE>32</SERVICE_TYPE><START_TIME>20101004000000</START_TIME><TOTAL_FEE>0</TOTAL_FEE><BUSINESS_CODE>32360002</BUSINESS_CODE><SP_NAME>���ݹ�����湫˾</SP_NAME><MT_FEE> </MT_FEE><XFEE> </XFEE><SP_CODE>62910</SP_CODE><SEND_TYPE>1</SEND_TYPE><BILL_TYPE>2</BILL_TYPE><DURATION>0</DURATION><SERVICE_NAME>��Ե����ҵ��</SERVICE_NAME><MT_COUNT>0</MT_COUNT><OTHER_PARTY>13606609996</OTHER_PARTY></CDR_INFO><CDR_INFO><BILL_NUMBER>15657177218</BILL_NUMBER><SERVICE_TYPE>32</SERVICE_TYPE><START_TIME>20101004000000</START_TIME><TOTAL_FEE>0</TOTAL_FEE><BUSINESS_CODE>32360002</BUSINESS_CODE><SP_NAME>���ݹ�����湫˾</SP_NAME><MT_FEE> </MT_FEE><XFEE> </XFEE><SP_CODE>62910</SP_CODE><SEND_TYPE>1</SEND_TYPE><BILL_TYPE>2</BILL_TYPE><DURATION>0</DURATION><SERVICE_NAME>��Ե����ҵ��</SERVICE_NAME><MT_COUNT>0</MT_COUNT><OTHER_PARTY>13868068762</OTHER_PARTY></CDR_INFO><CDR_INFO><BILL_NUMBER>15657177218</BILL_NUMBER><SERVICE_TYPE>32</SERVICE_TYPE><START_TIME>20101004000000</START_TIME><TOTAL_FEE>0</TOTAL_FEE><BUSINESS_CODE>32360002</BUSINESS_CODE><SP_NAME>���ݹ�����湫˾</SP_NAME><MT_FEE> </MT_FEE><XFEE> </XFEE><SP_CODE>62910</SP_CODE><SEND_TYPE>1</SEND_TYPE><BILL_TYPE>2</BILL_TYPE><DURATION>0</DURATION><SERVICE_NAME>��Ե����ҵ��</SERVICE_NAME><MT_COUNT>0</MT_COUNT><OTHER_PARTY>13634170668</OTHER_PARTY></CDR_INFO><CDR_INFO><BILL_NUMBER>15657177218</BILL_NUMBER><SERVICE_TYPE>32</SERVICE_TYPE><START_TIME>20101016000000</START_TIME><TOTAL_FEE>0</TOTAL_FEE><BUSINESS_CODE>32360002</BUSINESS_CODE><SP_NAME>���ݹ�����湫˾</SP_NAME><MT_FEE> </MT_FEE><XFEE> </XFEE><SP_CODE>62910</SP_CODE><SEND_TYPE>1</SEND_TYPE><BILL_TYPE>2</BILL_TYPE><DURATION>0</DURATION><SERVICE_NAME>��Ե����ҵ��</SERVICE_NAME><MT_COUNT>0</MT_COUNT><OTHER_PARTY>13858000617</OTHER_PARTY></CDR_INFO><CDR_INFO><BILL_NUMBER>15657177218</BILL_NUMBER><SERVICE_TYPE>32</SERVICE_TYPE><START_TIME>20101017000000</START_TIME><TOTAL_FEE>0</TOTAL_FEE><BUSINESS_CODE>32360002</BUSINESS_CODE><SP_NAME>���ݹ�����湫˾</SP_NAME><MT_FEE> </MT_FEE><XFEE> </XFEE><SP_CODE>62910</SP_CODE><SEND_TYPE>1</SEND_TYPE><BILL_TYPE>2</BILL_TYPE><DURATION>0</DURATION><SERVICE_NAME>��Ե����ҵ��</SERVICE_NAME><MT_COUNT>0</MT_COUNT><OTHER_PARTY>13868068762</OTHER_PARTY></CDR_INFO><CDR_INFO><BILL_NUMBER>15657177218</BILL_NUMBER><SERVICE_TYPE>32</SERVICE_TYPE><START_TIME>20101030000000</START_TIME><TOTAL_FEE>0</TOTAL_FEE><BUSINESS_CODE>32360002</BUSINESS_CODE><SP_NAME>���ݹ�����湫˾</SP_NAME><MT_FEE> </MT_FEE><XFEE> </XFEE><SP_CODE>62910</SP_CODE><SEND_TYPE>1</SEND_TYPE><BILL_TYPE>2</BILL_TYPE><DURATION>0</DURATION><SERVICE_NAME>��Ե����ҵ��</SERVICE_NAME><MT_COUNT>0</MT_COUNT><OTHER_PARTY>13868068762</OTHER_PARTY></CDR_INFO><RESP_CODE>0000</RESP_CODE></QRY_CDR_OF_VA_INFO_RSP></UNI_BSS_BODY>";
//		String s = "H4sIAAAAAAAAAO2W0UrDMBSGX8UXGGliuzk4RNoklWDX1CR19CpPp7deCIKIt4J66SsIPoQ3pinbOtxwyG6c7VXOn/+cnJyPQqAupcuMcZniDYUL3TjGtVO546lNnSxz5bSpKGhhKseFYfT2+f4a0CqGNqE1UuDCsVwIGgFaLCGTReHKepYJTXEyTiZ4MiVj7+hvQPBiQF2OEfpSMuFsUwk6enp//QS0pkHKmPBdl8LOlT7vRF9zkwxW2bRweaHmraUXgbGpts7KmaAkwlE0jqbR4jv2Au6W/vCVcVGu63cVgFbprDvxyI9nGYAoechEFLyvEwmg5Rp4rVMrVekNYehtb66uwhT7MUjPqlAsmB2TtqGngL6LvSpczcv1OkHxc14yG+gdFj3yS3xt3gZ+MR6dVdocPVzd3by8PX7sCSUmcbI7TfIzzWgLTbwfmjFJ9sTzhAxEd/o/B6J/img8ED0wosk/Ihqes0xxf6Vw3VUMaPujGK29nr8AY3ETTEwLAAA=";
//
//		String bb = "H4sIAAAAAAAAAA==";
//		try {
//			byte[] b = compress(s.getBytes());
//			System.out.println(u.uncompress(s));
//			// System.out.println(s.length());
//			// System.out.println(b.length);
//			// String s1 = new String(decompress(b));
//			// System.out.println(s1);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//	}

	public static byte[] decompress(byte[] data) throws Exception {
		ByteArrayInputStream bais = new ByteArrayInputStream(data);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		decompress(bais, baos);

		data = baos.toByteArray();

		baos.flush();
		baos.close();

		bais.close();

		return data;
	}

	public static void decompress(InputStream is, OutputStream os) throws Exception {

		GZIPInputStream gis = new GZIPInputStream(is);
		int count;
		byte data[] = new byte[1024];
		while ((count = gis.read(data, 0, 1024)) != -1) {
			os.write(data, 0, count);
		}

		gis.close();
//		String s = gis.toString();
	}

//	public static byte[] compress(byte[] data) throws Exception {
//		ByteArrayInputStream bais = new ByteArrayInputStream(data);
//		ByteArrayOutputStream baos = new ByteArrayOutputStream();
//
//		compress(bais, baos);
//
//		byte[] output = baos.toByteArray();
//		String s = baos.toString();
//		baos.flush();
//		baos.close();
//
//		bais.close();
//
//		return output;
//	}

	public static void compress(InputStream is, OutputStream os) throws Exception {

		GZIPOutputStream gos = new GZIPOutputStream(os);

		int count;
		byte data[] = new byte[1024];
		while ((count = is.read(data, 0, 1024)) != -1) {
			gos.write(data, 0, count);
		}

		gos.finish();

		gos.flush();
		gos.close();
	}

}
